<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="../css/style.css">
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <script src="../js/vue.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <script src="../js/jquery.min.js"></script>
    <script src="../plugins/elementui/index.js"></script>

</head>
<body>
<div id="table">


    <el-input v-model="user.username" placeholder="根据用户查询" style="width: 200px;">
        <template slot="prepend">用户：</template>
    </el-input>

    <el-input v-model="user.gender" placeholder="根据性别查询" style="width: 200px;">
        <template slot="prepend">性别：</template>
    </el-input>

    <el-input v-model="user.email" placeholder="根据邮箱查询" style="width: 200px;">
        <template slot="prepend">邮箱：</template>
    </el-input>

    <el-button @click="newPage">查询</el-button>
    <el-button @click="add" style="float: right">添加</el-button>
    <el-table :data="users" stripe>
        <el-table-column prop="id" label="序号"></el-table-column>
        <el-table-column prop="username" label="姓名"></el-table-column>
        <el-table-column prop="gender" label="性别"></el-table-column>
        <el-table-column prop="password" label="密码"></el-table-column>
        <el-table-column prop="email" label="邮箱"></el-table-column>
        <el-table-column label="操作" align="center">
            <!--
            slot-scope：作用域插槽，可以获取表格数据
            scope：代表表格数据，可以通过scope.row来获取表格当前行数据，scope不是固定写法
            -->
            <template slot-scope="scope">
                <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑
                </el-button>
                <el-button type="danger" size="mini" @click="handleDelete(scope.row)">删除
                </el-button>
            </template>
        </el-table-column>
    </el-table>

    <div class="block">
        <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="pagination.currentPage"
                :page-sizes="[5,10,15,20]"
                :page-size="pagination.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="pagination.total"
                style="float: right">
        </el-pagination>
    </div>
    <el-dialog
            title="添加用户"
            :visible.sync="addDialogVisible"
            width="30%"
    >
        <el-form :model="formData">
            <el-row>
                <el-col>
                    <el-form-item label="序号">
                        <el-input v-model="formData.id"/>
                        <template slot="prepend">序号：</template>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col>
                    <el-form-item label="用户名">
                        <el-input v-model="formData.username"/>
                        <template slot="prepend">用户名：</template>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col>
                    <el-form-item label="性别">
                        <el-input v-model="formData.gender"/>
                        <template slot="prepend">性别：</template>

                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col>
                    <el-form-item label="密码">
                        <el-input v-model="formData.password"/>
                        <template slot="prepend">密码：</template>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col>
                    <el-form-item label="邮箱">
                        <el-input v-model="formData.email"/>
                        <template slot="prepend">邮箱：</template>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <span slot="footer" class="dialog-footer">
    <el-button @click="addDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="handleAdd">确 定</el-button>
  </span>
    </el-dialog>
    <el-dialog
            title="修改用户"
            :visible.sync="updateDialogVisible"
            width="30%"
    >
        <el-form :model="formData">
            <el-row>
                <el-col>
                    <el-form-item label="序号">
                        <el-input v-model="formData.id" :disabled="true"/>
                        <template slot="prepend">序号：</template>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col>
                    <el-form-item label="用户名">
                        <el-input v-model="formData.username"/>
                        <template slot="prepend">用户名：</template>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col>
                    <el-form-item label="性别">
                        <el-input v-model="formData.gender"/>
                        <template slot="prepend">性别：</template>

                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col>
                    <el-form-item label="密码">
                        <el-input v-model="formData.password"/>
                        <template slot="prepend">密码：</template>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col>
                    <el-form-item label="邮箱">
                        <el-input v-model="formData.email"/>
                        <template slot="prepend">邮箱：</template>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <span slot="footer" class="dialog-footer">
    <el-button @click="updateDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="handleEdit">确 定</el-button>
  </span>
    </el-dialog>
</div>
<script>
    var vue = new Vue({
        el: '#table',
        data: {
            addDialogVisible: false,
            updateDialogVisible: false,
            pagination: {
                total: 0,
                currentPage: 1,
                pageSize: 5
            },
            users: [],
            user: {
                "username": "",
                "gender": "",
                "email": "",
            },
            formData: {}
        },
        created() {
            this.newPage();
        },
        methods: {
            handleSizeChange(pageSize) {
                this.pagination.pageSize = pageSize;
                this.newPage();

            },
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.newPage();
            },
            handleUpdate(row) {
                this.formData = {};
                axios.get("/users/" + row.id).then(res => {
                    if (res.data.flag === true) {
                        this.updateDialogVisible = true;
                        this.$message.success(res.data.message);
                        this.formData = res.data.data;
                    } else {
                        this.$message.error(res.data.message);
                    }
                })
            },
            handleDelete(row) {

                this.$confirm('此操作永久删除当前信息，是否继续？', '提示', {type: 'warning'}).then(() => {
                    axios.delete("/users/" + row.id).then(res => {
                        if (res.data.flag === true) {
                            this.$message.success(res.data.message);
                        } else {
                            this.$message.error(res.data.message);
                        }
                    })
                    this.newPage();
                })
            },
            newPage() {
                axios.post("/users/" + this.pagination.currentPage + "/" + this.pagination.pageSize, this.user).then(res => {
                    if (res.data.flag === true) {
                        this.pagination.total = res.data.data.total;
                        this.users = res.data.data.r;
                    } else {
                        this.$message.error(res.data.message);
                    }

                })
            },
            add() {
                this.addDialogVisible = true;
                this.formData = {}
            },
            handleAdd() {
                axios.post("/users", this.formData).then(res => {
                    if (res.data.flag === true) {
                        this.$message.success(res.data.message);
                    } else {
                        this.$message.error(res.data.message);
                    }
                });
                this.newPage();
                this.addDialogVisible = false;
            },
            handleEdit() {
                axios.put("/users", this.formData).then(res => {
                    if (res.data.flag === true) {
                        this.$message.success(res.data.message);
                    } else {
                        this.$message.error(res.data.message);
                    }
                });
                this.newPage();
                this.updateDialogVisible = false;
            }
        }
    });
</script>

</body>
</html>